<?php

class m120503_072333_create_post extends CDbMigration {

	public function safeUp() {
		$post = array(
			'id'=>'pk',
			'userId'=>'integer',
			'name'=>'string',
			'email'=>'varchar(128)',
			'value'=>'text',
			'viewCount'=>'integer',
			'createdAt'=>'datetime',
			'updatedAt'=>'datetime',
		);
		$this->createTable('posts', $post);
		$this->createIndex('index_post_user', 'posts', 'userId');
		$this->createIndex(
			'index_post_updated_at', 'posts', 'updatedAt'
		);

		$auth = Yii::app()->authManager;

		$auth->createOperation('postUpdate', 'Update Post');
		$auth->createOperation('postDelete', 'Delete Post');
		$auth->createOperation('postManage', 'Manage Post');

		$role = $auth->createRole('postModerator', 'Post Moderator');
		$role->addChild('postUpdate');
		$role->addChild('postDelete');
		$role->addChild('postManage');

		$auth->addItemChild('admin', 'postModerator');
	}

	public function safeDown() {
		$this->dropTable('posts');

		$auth = Yii::app()->authManager;

		$auth->removeItemChild('admin', 'postModerator');

		$auth->removeItemChild('postModerator', 'postUpdate');
		$auth->removeItemChild('postModerator', 'postDelete');
		$auth->removeItemChild('postModerator', 'postManage');

		$auth->removeAuthItem('postUpdate');
		$auth->removeAuthItem('postDelete');
		$auth->removeAuthItem('postManage');

		$auth->removeAuthItem('postModerator');
	}

}